package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.pawnshop.*;
import com.yanqu.road.entity.enums.activity.pawnshop.ePawnshopEventType;
import com.yanqu.road.pb.activity.PawnshopProto;
import com.yanqu.road.server.manger.activity.pawnshop.PawnshopMgr;

import java.util.ArrayList;
import java.util.List;

public class PawnshopPb {

    public static PawnshopProto.DPawnshopTempMsg.Builder parseDPawnshopTempMsg(ConfPawnshop confPawnshop){
        if(null == confPawnshop){
            return null;
        }
        PawnshopProto.DPawnshopTempMsg.Builder msg = PawnshopProto.DPawnshopTempMsg.newBuilder();
        msg.setLevel(confPawnshop.getLevel());
        msg.setIcon(confPawnshop.getIcon());
        msg.setUpgradeWealth(confPawnshop.getUpgradeMoney());
        msg.setUpgradeReward(confPawnshop.getUpgradeReward());
        msg.setStaminaCost(confPawnshop.getStaminaCost());
        msg.setItemCount(confPawnshop.getItemCount());
        msg.setMoneyIcon(confPawnshop.getMoneyIcon());
        msg.setRefusePower(confPawnshop.getRefusePower());
        msg.setUpgradeOtherReward(confPawnshop.getUpgradeOtherReward());
        return msg;
    }

    public static PawnshopProto.DPawnshopStaff.Builder parseDPawnshopStaff(ConfPawnshopStaff confStaff) {
        if(null == confStaff){
            return null;
        }
        PawnshopProto.DPawnshopStaff.Builder msg = PawnshopProto.DPawnshopStaff.newBuilder();
        msg.setType(confStaff.getType());
        msg.setLevel(confStaff.getLevel());
        msg.setLevelUnlock(confStaff.getUnlockLevel());
        msg.setParam1(confStaff.getParam1());
        msg.setParam2(confStaff.getParam2());
        msg.setParamWeight(confStaff.getParamWeight());
        return msg;
    }

    public static PawnshopProto.PawnshopStaffTempMsg.Builder parsePawnshopStaffTempMsg(UserPawnshopStaff staff){
        if(null == staff){
            return null;
        }
        PawnshopProto.PawnshopStaffTempMsg.Builder msg = PawnshopProto.PawnshopStaffTempMsg.newBuilder();
        msg.setType(staff.getStaffType());
        msg.setLevel(staff.getLevel());
        return msg;
    }

    public static PawnshopProto.DPawnshopItem.Builder parseDPawnshopItem(UserPawnshopItem userItem){
        if(null == userItem){
            return null;
        }
        PawnshopProto.DPawnshopItem.Builder msg = PawnshopProto.DPawnshopItem.newBuilder();
        msg.setItemId(userItem.getItemId());
        msg.setOpStatus(userItem.getOpStatus());
        msg.setOpReceptionNum(userItem.getOpReceptionNum());
        msg.setRepairTimes(userItem.getRepairTimes());
        msg.setFakeStar(userItem.getFakeStar());
        msg.setGoodsId(userItem.getGoodsId());
        msg.setStoneGoodsId(0);
        msg.setStoneLv(0);
        msg.setLevel(userItem.getLevel());
        msg.setRarity(userItem.getRarity());
        msg.setStatus(userItem.getStatus());
        msg.setCoinBuy(userItem.getCoinBuy());
        msg.setAffixSn1(userItem.getAffixSn1());
        msg.setAffixSn2(userItem.getAffixSn2());
        msg.setAffixSn3(userItem.getAffixSn3());
        msg.setAffixSn4(userItem.getAffixSn4());
        msg.setBasePrice(userItem.getBasePrice());
        msg.setStatusEffectPrice(userItem.getStatusEffectPrice());
        msg.setEventAffixAddition(userItem.getEventAffixAddition());
        msg.setPersonAffixAddition(userItem.getPersonAffixAddition());
        msg.setDescNo(userItem.getDescNo());
        return msg;
    }

    public static PawnshopProto.DPawnshopItem.Builder parseDPawnshopItem(PawnshopItem pawnshopItem){
        if(null == pawnshopItem){
            return null;
        }
        PawnshopProto.DPawnshopItem.Builder msg = PawnshopProto.DPawnshopItem.newBuilder();
        msg.setItemId(pawnshopItem.getItemId());
        msg.setOpStatus(0);
        msg.setOpReceptionNum(0);
        msg.setRepairTimes(0);
        msg.setFakeStar(0);
        msg.setGoodsId(pawnshopItem.getGoodsId());
        msg.setStoneGoodsId(pawnshopItem.getStoneGoodsId());
        msg.setStoneLv(pawnshopItem.getStoneLv());
        msg.setLevel(pawnshopItem.getLevel());
        msg.setRarity(pawnshopItem.getRarity());
        msg.setStatus(pawnshopItem.getStatus());
        msg.setCoinBuy(pawnshopItem.getCoinBuy());
        msg.setAffixSn1(pawnshopItem.getAffixSn1());
        msg.setAffixSn2(pawnshopItem.getAffixSn2());
        msg.setAffixSn3(pawnshopItem.getAffixSn3());
        msg.setAffixSn4(pawnshopItem.getAffixSn4());
        msg.setBasePrice(pawnshopItem.getBasePrice());
        msg.setStatusEffectPrice(pawnshopItem.getStatusEffectPrice());
        msg.setEventAffixAddition(pawnshopItem.getEventAffixAddition());
        msg.setPersonAffixAddition(pawnshopItem.getPersonAffixAddition());
        msg.setDescNo(pawnshopItem.getDescNo());
        msg.setFakeStar(pawnshopItem.getFakeStar());
        return msg;
    }

    public static PawnshopProto.DPawnshopItemTempMsg.Builder parseDPawnshopItemTempMsg(ConfPawnshopItem confPawnshopItem){
        if(null == confPawnshopItem){
            return null;
        }
        PawnshopProto.DPawnshopItemTempMsg.Builder msg = PawnshopProto.DPawnshopItemTempMsg.newBuilder();
        msg.setGoodsId(confPawnshopItem.getId());
        msg.setIcon(confPawnshopItem.getIcon());
        msg.setType(confPawnshopItem.getType());
        return msg;
    }

    public static PawnshopProto.DPawnshopItemLevelTempMsg.Builder parseDPawnshopItemLevelTempMsg(ConfPawnshopItemLv confPawnshopItemLv){
        if(null == confPawnshopItemLv){
            return null;
        }
        PawnshopProto.DPawnshopItemLevelTempMsg.Builder msg = PawnshopProto.DPawnshopItemLevelTempMsg.newBuilder();
        msg.setLevel(confPawnshopItemLv.getLevel());
        msg.setMoneyIcon(confPawnshopItemLv.getMoneyIcon());
        return msg;
    }

//    public static PawnshopProto.PawnshopBasicsDataMsg.Builder parsePawnshopBasicsDataMsg(UserPawnshop userPawnshop){
//        if(null == userPawnshop){
//            return null;
//        }
//        PawnshopProto.PawnshopBasicsDataMsg.Builder msg = PawnshopProto.PawnshopBasicsDataMsg.newBuilder();
//        msg.setReceptionNum(userPawnshop.getReceptionNum());
//        msg.setLevel(userPawnshop.getLevel());
//        msg.setCoinUsable(userPawnshop.getCoinUsable());
//        msg.setCoinTotal(userPawnshop.getCoinTotal());
//        msg.setWashTimes(userPawnshop.getWashTimes());
//        return msg;
//    }

    public static PawnshopProto.DPawnshopPolitics.Builder parseDPawnshopPolitics(UserPawnshop userPawnshop) {
        if(null == userPawnshop){
            return null;
        }
        PawnshopProto.DPawnshopPolitics.Builder msg = PawnshopProto.DPawnshopPolitics.newBuilder();
        msg.setPoliticsEventId(userPawnshop.getPoliticsEventId());
        msg.setTriggerReceptionNum(userPawnshop.getPoliticsStartReceptionNum());
        msg.setAddition(userPawnshop.getPoliticsAddition());
        msg.setIsRead(userPawnshop.isPoliticsRead());
        return msg;
    }

    public static PawnshopProto.DPawnshopPoliticsEventTempMsg.Builder parseDPawnshopPoliticsEventTempMsg(ConfPawnshopEvent confPawnshopEvent){
        if(null == confPawnshopEvent){
            return null;
        }
        PawnshopProto.DPawnshopPoliticsEventTempMsg.Builder msg = PawnshopProto.DPawnshopPoliticsEventTempMsg.newBuilder();
        msg.setPoliticsEventId(confPawnshopEvent.getEventId());
        msg.setDuringDays(confPawnshopEvent.getDuring());
        return msg;
    }

    public static PawnshopProto.DPawnshopNotify.Builder parseDPawnshopNotify(UserPawnshopNotify userPawnshopNotify){
        if(null == userPawnshopNotify){
            return null;
        }
        PawnshopProto.DPawnshopNotify.Builder msg = PawnshopProto.DPawnshopNotify.newBuilder();
        msg.setNotifyId(userPawnshopNotify.getNotifyId());
        msg.setType(userPawnshopNotify.getType());
        msg.setItemId(userPawnshopNotify.getItemId());
        msg.setCoinValuationOld(userPawnshopNotify.getCoinValuationOld());
        msg.setStatusOld(userPawnshopNotify.getStatusOld());
        msg.setRedemptionPrice(userPawnshopNotify.getRedemptionPrice());
        PawnshopProto.DPawnshopItem.Builder itemMsg = parseDPawnshopItem(userPawnshopNotify.getUserPawnshopItem());
        if(null != itemMsg){
            msg.setPawnshopItem(itemMsg);
        }
        return msg;
    }

    public static List<PawnshopProto.DPawnshopNotify> parseDPawnshopNotifyList(List<UserPawnshopNotify> notifyList){
        List<PawnshopProto.DPawnshopNotify> listMsg = new ArrayList<>();
        if(null != notifyList){
            for(UserPawnshopNotify notify : notifyList){
                PawnshopProto.DPawnshopNotify.Builder msg = parseDPawnshopNotify(notify);
                if(null != msg){
                    listMsg.add(msg.build());
                }
            }
        }
        return listMsg;
    }

    public static PawnshopProto.DPawnshopEvent.Builder getDPawnshopEvent(UserPawnshopEvent userEvent) {
        PawnshopProto.DPawnshopEvent.Builder d = PawnshopProto.DPawnshopEvent.newBuilder();
        d.setType(userEvent.getType());
        d.setNpcLv(userEvent.getNpc().getLv());
        // 事件数据
        ePawnshopEventType type = ePawnshopEventType.forValue(userEvent.getType());
        switch (type) {
            case Sell:
            case Buy: {
                PawnshopProto.DPawnshopEventBuySell.Builder dBuySell = PawnshopProto.DPawnshopEventBuySell.newBuilder();
                List<PawnshopItem> itemList = userEvent.getItemList();
                if (!itemList.isEmpty()) {
                    PawnshopItem pawnshopItem = itemList.get(0);
                    PawnshopProto.DPawnshopItem.Builder itemMsg = PawnshopPb.parseDPawnshopItem(pawnshopItem);
                    dBuySell.setDItem(itemMsg);
                }
                dBuySell.setIsFake(userEvent.getIsFake());
                dBuySell.setWashTimes(userEvent.getWashTime());
                dBuySell.setUserBid(userEvent.getUserBid());
                dBuySell.setStatus(userEvent.getStatus());
                dBuySell.setPatient(userEvent.getPatient());
                for (PawnshopBidPrice price : userEvent.getPriceList()) {
                    dBuySell.addPriceList(PawnshopPb.parseBidPrice(price));
                }
                d.setDEventBuySell(dBuySell);
            }
            break;
            case Hock: {
                PawnshopProto.DPawnshopEventHock.Builder dHock = PawnshopProto.DPawnshopEventHock.newBuilder();
                List<PawnshopItem> itemList = userEvent.getItemList();
                if (!itemList.isEmpty()) {
                    PawnshopItem pawnshopItem = itemList.get(0);
                    PawnshopProto.DPawnshopItem.Builder itemMsg = PawnshopPb.parseDPawnshopItem(pawnshopItem);
                    dHock.setDItem(itemMsg);
                }
                dHock.setBuyRate(PawnshopMgr.config.eventHockBuyRate);
                d.setDEventHock(dHock);
            }
            break;
            case Money: {
                PawnshopProto.DPawnshopEventMoney.Builder dMoney = PawnshopProto.DPawnshopEventMoney.newBuilder();
                dMoney.setMoney(userEvent.getMoney().getMoney());
                d.setDEventMoney(dMoney);
            }
            break;
            case Gemstone: {
                PawnshopProto.DPawnshopEventGemstone.Builder dGemstone = PawnshopProto.DPawnshopEventGemstone.newBuilder();
                List<PawnshopItem> itemList = userEvent.getItemList();
                for (PawnshopItem pawnshopItem : itemList) {
                    PawnshopProto.DPawnshopItem.Builder itemMsg = PawnshopPb.parseDPawnshopItem(pawnshopItem);
                    if(null != itemMsg) {
                        dGemstone.addDItemList(itemMsg);
                    }
                }
                d.setDEventGemstone(dGemstone);
            }
            break;
        }
        return d;
    }



    public static PawnshopProto.PawnshopStaffListMsg.Builder parsePawnshopStaffListMsg(List<UserPawnshopStaff> staffList){
        PawnshopProto.PawnshopStaffListMsg.Builder listMsg = PawnshopProto.PawnshopStaffListMsg.newBuilder();
        if(null != staffList){
            for(UserPawnshopStaff staff : staffList) {
                PawnshopProto.PawnshopStaffTempMsg.Builder msg = parsePawnshopStaffTempMsg(staff);
                if(null != msg){
                    listMsg.addStaffList(msg);
                }
            }
        }
        return listMsg;
    }

    public static  PawnshopProto.BidPriceMsg.Builder parseBidPrice(PawnshopBidPrice bidPrice) {
        PawnshopProto.BidPriceMsg.Builder msg = PawnshopProto.BidPriceMsg.newBuilder();
        msg.setPrice(bidPrice.getPrice());
        msg.setProbability(bidPrice.getProbability());
        msg.setReducePatient(bidPrice.getReducePatient());
        return msg;
    }
}
